#include "StdAfx.h"
#include "DrawConverHull2.h"


CDrawConverHull2::CDrawConverHull2(void)
{
}


CDrawConverHull2::~CDrawConverHull2(void)
{
}
BEGIN_MESSAGE_MAP(CDrawConverHull2, CDrawOpenCV)
	ON_WM_PAINT()
END_MESSAGE_MAP()


void CDrawConverHull2::DealwithFrame()
{
	if(m_pFrame == NULL)
	{			
		return ;
	}
	if(m_pShow != NULL)
	{
		cvReleaseImage(&m_pShow);
	} 

	m_pShow = cvCreateImage( cvSize( m_pFrame->width, m_pFrame->height), 8, 3 );


	CvMemStorage* storage = cvCreateMemStorage();

	int i, count = rand()%100 + 1, hullcount;
	CvPoint pt0;

	CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
		sizeof(CvPoint), storage );
	CvSeq* hull;

	for( i = 0; i < count; i++ )
	{
		pt0.x = rand() % (m_pShow->width/2) + m_pShow->width/4;
		pt0.y = rand() % (m_pShow->height/2) + m_pShow->height/4;
		cvSeqPush( ptseq, &pt0 );
	}
	hull = cvConvexHull2( ptseq, 0, CV_CLOCKWISE, 0 );
	hullcount = hull->total;

	cvZero( m_pShow );
	for( i = 0; i < count; i++ )
	{
		pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
		cvCircle( m_pShow, pt0, 3, CV_RGB( 255, 0, 0 ), CV_FILLED );
	}

	pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hullcount - 1 );

	for( i = 0; i < hullcount; i++ )
	{
		CvPoint pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, i );
		cvLine( m_pShow, pt0, pt, CV_RGB( 0, 255, 0 ));
		pt0 = pt;
	}
	cvReleaseMemStorage(&storage);
//	cvClearMemStorage( storage );
}
